<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="型号" prop="pattern">
        <el-select v-model="queryParams.pattern" style="width: 192px;" placeholder="请选择型号" clearable>
          <el-option
              v-for="dict in weixing_xinghao"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编码" prop="code">
        <el-input
            v-model="queryParams.code"
            placeholder="请输入编码"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" style="width: 192px;"  placeholder="请选择状态" clearable>
          <el-option
              v-for="dict in weixing_gl_zt"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="操作者" prop="operatorId">
        <el-input
            v-model="queryParams.operatorId"
            placeholder="请输入操作者"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间" style="width: 308px">
        <el-date-picker
            v-model="daterangeTimes"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['satellite:satellitemanage:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['satellite:satellitemanage:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['satellite:satellitemanage:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['satellite:satellitemanage:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="satellitemanageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id"  v-if="false"  />
      <el-table-column label="型号" align="center" prop="pattern">
        <template #default="scope">
          <dict-tag :options="weixing_xinghao" :value="scope.row.pattern"/>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="编码" align="center" prop="code" />
      <el-table-column label="状态" align="center" prop="state">
        <template #default="scope">
          <dict-tag :options="weixing_gl_zt" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="操作者" align="center" prop="operatorId" />
      <el-table-column label="时间" align="center" prop="times" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.times, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容说明" align="center" prop="comment" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['satellite:satellitemanage:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['satellite:satellitemanage:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改卫星管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="satellitemanageRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="型号" prop="pattern">
          <el-select v-model="form.pattern" placeholder="请选择型号">
            <el-option
                v-for="dict in weixing_xinghao"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入请输入十六进制字节，如0xA1, 0x01的编码" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择状态">
            <el-option
                v-for="dict in weixing_gl_zt"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作者" prop="operatorId">
          <el-input v-model="form.operatorId" placeholder="请输入操作者" />
        </el-form-item>
        <el-form-item label="时间" prop="times">
          <el-date-picker clearable
                          v-model="form.times"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="内容说明">
          <editor v-model="form.comment" :min-height="192"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Satellitemanage">
import { listSatellitemanage,
  listSatellitemanageSS,
  getSatellitemanage,
  delSatellitemanage,
  addSatellitemanage,
  updateSatellitemanage } from "@/api/satellite/satellitemanage";

const { proxy } = getCurrentInstance();
const { weixing_xinghao, weixing_gl_zt } = proxy.useDict('weixing_xinghao', 'weixing_gl_zt');

const satellitemanageList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeTimes = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    pattern: null,
    name: null,
    code: null,
    state: null,
    operatorId: null,
    times: null,
  },
  rules: {
    pattern: [
      { required: true, message: "型号不能为空", trigger: "change" }
    ],
    name: [
      { required: true, message: "名称不能为空", trigger: "blur" }
    ],
    code: [
      { required: true, message: "编码不能为空", trigger: "blur" },
      {
        pattern: /^0x[0-9a-fA-F]{2}$/,
        message: "请输入合法的十六进制字节，如 0xA1",
        trigger: "blur"
      }
    ],
    state: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
    operatorId: [
      { required: true, message: "操作者不能为空", trigger: "blur" }
    ],
    times: [
      { required: true, message: "时间不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询卫星管理列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeTimes && '' != daterangeTimes) {
    queryParams.value.params["beginTimes"] = daterangeTimes.value[0];
    queryParams.value.params["endTimes"] = daterangeTimes.value[1];
  }
  listSatellitemanageSS(queryParams.value).then(response => {
    satellitemanageList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    pattern: null,
    name: null,
    code: null,
    state: null,
    operatorId: null,
    times: null,
    comment: null
  };
  proxy.resetForm("satellitemanageRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeTimes.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加卫星管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getSatellitemanage(_id).then(response => {
    console.log('修改按钮操作:',response);
    form.value = response.data;
    open.value = true;
    title.value = "修改卫星管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["satellitemanageRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateSatellitemanage(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSatellitemanage(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除卫星管理编号为"' + _ids + '"的数据项？').then(function() {
    return delSatellitemanage(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('apis/satellitemanage/export', {
    ...queryParams.value
  }, `satellitemanage_${new Date().getTime()}.xlsx`)
}

getList();
</script>
